package com.yaowk.platform.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.yaowk.authc.model.User;
import com.yaowk.common.kit.ResultKit;
import com.yaowk.common.validator.RequiredIdValidator;
import com.yaowk.platform.model.Qualification;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.util.List;

/**
 * 资质
 *
 * @authc yaowk
 * 2017/5/1
 */
public class QualificationController extends Controller {

    /**
     * 资质列表
     *
     * @param id|平台id|Integer|必填
     * @title 资质列表
     */
    @Before(RequiredIdValidator.class)
    @RequiresPermissions("platform:read")
    public void list() {
        Integer platformId = getParaToInt("id");
        List<Qualification> qualificationList = Qualification.dao.findByPlatformId(platformId);
        renderJson(ResultKit.success(qualificationList));
    }

    /**
     * 添加资质
     *
     * @param platform_id|平台id|Integer|必填
     * @param name|资质简称|String|否
     * @param full_name|资质全称|String|否
     * @param validity_time|有效期1-短期2-长期|Integer|必填
     * @param sort|排序|Integer|否
     * @title 添加资质
     */
    @RequiresPermissions("platform:update")
    public void add() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Qualification qualification = getModel(Qualification.class, "");
        qualification.setUserId(user.getId()).save();
        renderJson(ResultKit.success(this));
    }

    /**
     * 编辑资质
     *
     * @param id|id|Integer|必填
     * @param validity_time|有效期1-短期2-长期|Integer|必填
     * @param name|资质简称|String|否
     * @param full_name|资质全称|String|否
     * @param sort|排序|Integer|否
     * @title 编辑资质
     */
    @Before(RequiredIdValidator.class)
    @RequiresPermissions("platform:update")
    public void edit() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Qualification qualification = getModel(Qualification.class, "");
        Qualification old = Qualification.dao.findById(qualification.getId());
        old.setName(qualification.getName())
                .setUserId(user.getId())
                .setFullName(qualification.getFullName())
                .setSort(qualification.getSort())
                .setValidityTime(qualification.getValidityTime())
                .update();
        renderJson(ResultKit.success(this));
    }

    /**
     * 删除资质
     *
     * @param id|id|Integer|必填
     * @title 删除资质
     */
    @RequiresPermissions("platform:update")
    public void remove() {
        Integer id = getParaToInt("id");
        Qualification.dao.findById(id).setStatus(0).update();
        renderJson(ResultKit.success(this));
    }
}
